<?php $this->beginWidget('webroot.widgets.iButtonBar');?>

<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>Yii::t('main','Save'),
    'type'=>'success',
	'icon'=>'ok-sign white',
    'url'=> 'javascript:void(0);',
	'htmlOptions'=>array('onclick'=>'jQuery("#profile-form").submit();')
));?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>Yii::t('main', 'Save & Continue'),
    'type'=>'primary',
	'icon'=>'ok white',
    'url'=> 'javascript:void(0);',
	'htmlOptions'=>array('onclick'=>'if(jQuery("#apply").attr("value",1)) jQuery("#profile-form").submit();')
));?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>Yii::t('main','Change password'),
    'type'=>'primary',
	'icon'=>'ok-sign white',
    'url'=> array('/user/profile/changepassword'),
));?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>Yii::t('main','View Profile'),
    'type'=>'primary',
	'icon'=>'eye-open white',
    'url'=> array('/user/profile'),
));?>
<?php $this->endWidget();?>
<?php 
$this->pageTitle=Yii::app()->name . ' - '.UserModule::t("Profile");
$this->beginWidget('webroot.widgets.iPortlet', array('title'=>UserModule::t('Edit profile')));
?>

<?php if(Yii::app()->user->hasFlash('profileMessage')): ?>
<div class="success">
<?php echo Yii::app()->user->getFlash('profileMessage'); ?>
</div>
<?php endif; ?>
<div class="form" style="overflow: hidden">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'profile-form',
	'enableAjaxValidation'=>false,
	'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>

	<?php echo $form->errorSummary(array($model,$profile)); ?>
<div style="float: left">
<?php 
		$profileFields=$profile->getFields();
		if ($profileFields) {
			foreach($profileFields as $field) {
			?>
	<div class="row">
		<?php echo $form->labelEx($profile,$field->varname);
		
		if ($widgetEdit = $field->widgetEdit($profile)) {
			echo $widgetEdit;
		} elseif ($field->range) {
			echo $form->dropDownList($profile,$field->varname,Profile::range($field->range));
		} elseif ($field->field_type=="TEXT") {
			echo $form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
		} else {
			echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
		}
		echo $form->error($profile,$field->varname); ?>
	</div>	
		<?php
			}
		}
		?>
	<div class="row">
		<?php echo $form->labelEx($model,'username'); ?>
		<?php echo $form->textField($model,'username',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($model,'username'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'email'); ?>
		<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'email'); ?>
	</div>
	</div>
	<div class="el-dialogform" style="float: right;width: 400px;margin-right: 10px;">
				<fieldset>
					<legend><label>Avatar</label></legend>
					
					<div id="uploaded_avatar">
						<?php if($model->avatar!=''):?>
						<img id="avatar" src="<?php echo Yii::app()->params['storage_url'].'/profile/'.$model->id.'/thumb/'.$model->avatar;?>" />
						<?php else:?>
						<img width="110" src="<?php echo Yii::app()->theme->baseUrl;?>/images/avatar.jpg" />
						<?php endif;?>
					</div>
					<br class="clear" />
					<div class="wb-upload">
					<?php $this->widget('MUploadify',array(
						'name'=>'User[avatar]',
						'buttonText'=>Yii::t('app','Upload Image'),
						'script'=>array('/user/profile/upload'),
						'model_id'=>$model->id,
						'fileExt'=>'*.jpg;*.png;',
						'auto'=>true,
					  //fileDesc=>Yii::t('application','Image files'),
					  //'uploadButton'=>true,
					  //'uploadButtonText'=>'Upload new',
					  //'uploadButtonTagname'=>'button',
					  //'uploadButtonOptions'=>array('class'=>'myButton'),
						'onComplete'=>'js:function(event, id, obj,respone, data){
							if(respone=="error_file_type"){
								alert("Upload lỗi.Kiểu file không hợp lệ.")
							}else if(respone=="error_file_upload"){
								alert("Lỗi upload.Không thể upload file.Hãy kiểm tra lại quyền thư mục upload file.")
							}else{
							 	$("#uploaded_avatar").html("<img src=\""+respone+"\" />");
							 	$("#wb-del-avatar").html("<a class=\"wb-del\" href=\"javascript:void(0);\" onclick=\"DelImage('.$model->id.');\" >'.Yii::t('app','Delete').'</a>");
							}
						}',
					));
					?>
					</div>
					<div class="toolb-i">
						<div id="wb-del-avatar">
							<?php if(!empty($model->avatar)):?>
							<a class="wb-del" href="javascript:void(0);" onclick="DelImage(<?php echo $model->id;?>);">
							<?php echo Yii::t('app','Delete');?>
							</a>
							<?php endif;?>
						</div>
					</div>
					
				</fieldset>
				</div>
	<input type="hidden" name="apply" id="apply" value="0" />

<?php $this->endWidget(); ?>

</div><!-- form -->
<?php 
$this->endWidget();
?>
<script type="text/javascript">
function DelImage(type,id){
	if(confirm('<?php echo Yii::t('main','Are you sure to want to delete this image?');?>')){
		jQuery.ajax({
			url: '<?php echo Yii::app()->createUrl('/user/profile/delimage');?>',
			data: {type:type, id:id},
			dataType:'json',
			success: function(data){
		   		if(data.error==false){
					jQuery("#wb-del-avatar").html("");   	
					jQuery("#uploaded_avatar").html("<img width=\"110\" src=\"<?php echo Yii::app()->theme->baseUrl;?>/images/avatar.jpg\">");   	
			  	}
			}
		})
	}
}
</script>